summaryrefslogtreecommitdiffstats
path: root/src/core/hle/service/cmif_types.h
blob: db5a013c7720dba6eeb3a3979b2f446c252347f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <memory>
#include <span>

#include "common/common_types.h"

namespace Service {

// clang-format off
template <typename T>
struct AutoOut {
    T raw;
};

template <typename T>
class Out {
public:
    using Type = T;

    /* implicit */ Out(const Out& t) : raw(t.raw) {}
    /* implicit */ Out(AutoOut<Type>& t) : raw(&t.raw) {}
    /* implicit */ Out(Type* t) : raw(t) {}
    Out& operator=(const Out&) = delete;

    Type* Get() const {
        return raw;
    }

    Type& operator*() const {
        return *raw;
    }

    Type* operator->() const {
        return raw;
    }

    operator Type*() const {
        return raw;
    }

private:
    Type* raw;
};

template <typename T>
using SharedPointer = std::shared_ptr<T>;

template <typename T>
using OutInterface = Out<SharedPointer<T>>;

struct ClientProcessId {
    explicit operator bool() const {
        return pid != 0;
    }

    const u64& operator*() const {
        return pid;
    }

    u64 pid;
};

struct ProcessId {
    explicit ProcessId() : pid() {}
    explicit ProcessId(u64 p) : pid(p) {}
    /* implicit */ ProcessId(const ClientProcessId& c) : pid(c.pid) {}

    bool operator==(const ProcessId& rhs) const {
        return pid == rhs.pid;
    }

    explicit operator bool() const {
        return pid != 0;
    }

    const u64& operator*() const {
        return pid;
    }

    u64 pid;
};

using ClientAppletResourceUserId = ClientProcessId;
using AppletResourceUserId = ProcessId;

template <typename T>
class InCopyHandle {
public:
    using Type = T;

    /* implicit */ InCopyHandle(Type* t) : raw(t) {}
    /* implicit */ InCopyHandle() : raw() {}
    ~InCopyHandle() = default;

    InCopyHandle& operator=(Type* rhs) {
        raw = rhs;
        return *this;
    }

    Type* Get() const {
        return raw;
    }

    Type& operator*() const {
        return *raw;
    }

    Type* operator->() const {
        return raw;
    }

    explicit operator bool() const {
        return raw != nullptr;
    }

private:
    Type* raw;
};

template <typename T>
class OutCopyHandle {
public:
    using Type = T*;

    /* implicit */ OutCopyHandle(const OutCopyHandle& t) : raw(t.raw) {}
    /* implicit */ OutCopyHandle(AutoOut<Type>& t) : raw(&t.raw) {}
    /* implicit */ OutCopyHandle(Type* t) : raw(t) {}
    OutCopyHandle& operator=(const OutCopyHandle&) = delete;

    Type* Get() const {
        return raw;
    }

    Type& operator*() const {
        return *raw;
    }

    Type* operator->() const {
        return raw;
    }

    operator Type*() const {
        return raw;
    }

private:
    Type* raw;
};

template <typename T>
class OutMoveHandle {
public:
    using Type = T*;

    /* implicit */ OutMoveHandle(const OutMoveHandle& t) : raw(t.raw) {}
    /* implicit */ OutMoveHandle(AutoOut<Type>& t) : raw(&t.raw) {}
    /* implicit */ OutMoveHandle(Type* t) : raw(t) {}
    OutMoveHandle& operator=(const OutMoveHandle&) = delete;

    Type* Get() const {
        return raw;
    }

    Type& operator*() const {
        return *raw;
    }

    Type* operator->() const {
        return raw;
    }

    operator Type*() const {
        return raw;
    }

private:
    Type* raw;
};

enum BufferAttr : int {
    /* 0x01 */ BufferAttr_In = (1U << 0),
    /* 0x02 */ BufferAttr_Out = (1U << 1),
    /* 0x04 */ BufferAttr_HipcMapAlias = (1U << 2),
    /* 0x08 */ BufferAttr_HipcPointer = (1U << 3),
    /* 0x10 */ BufferAttr_FixedSize = (1U << 4),
    /* 0x20 */ BufferAttr_HipcAutoSelect = (1U << 5),
    /* 0x40 */ BufferAttr_HipcMapTransferAllowsNonSecure = (1U << 6),
    /* 0x80 */ BufferAttr_HipcMapTransferAllowsNonDevice = (1U << 7),
};

template <typename T, int A>
struct Buffer : public std::span<T> {
    static_assert(std::is_trivially_copyable_v<T>, "Buffer type must be trivially copyable");
    static_assert((A & BufferAttr_FixedSize) == 0, "Buffer attr must not contain FixedSize");
    static_assert(((A & BufferAttr_In) == 0) ^ ((A & BufferAttr_Out) == 0), "Buffer attr must be In or Out");
    static constexpr BufferAttr Attr = static_cast<BufferAttr>(A);
    using Type = T;

    /* implicit */ Buffer(const std::span<T>& rhs) : std::span<T>(rhs) {}
    /* implicit */ Buffer() = default;

    Buffer& operator=(const std::span<T>& rhs) {
        std::span<T>::operator=(rhs);
        return *this;
    }

    T& operator*() const {
        return *this->data();
    }

    explicit operator bool() const {
        return this->size() > 0;
    }
};

template <int A>
using InBuffer = Buffer<const u8, BufferAttr_In | A>;

template <typename T, int A>
using InArray = Buffer<T, BufferAttr_In | A>;

template <int A>
using OutBuffer = Buffer<u8, BufferAttr_Out | A>;

template <typename T, int A>
using OutArray = Buffer<T, BufferAttr_Out | A>;

template <typename T, int A>
class InLargeData {
public:
    static_assert(std::is_trivially_copyable_v<T>, "LargeData type must be trivially copyable");
    static_assert((A & BufferAttr_Out) == 0, "InLargeData attr must not be Out");
    static constexpr BufferAttr Attr = static_cast<BufferAttr>(A | BufferAttr_In | BufferAttr_FixedSize);
    using Type = const T;

    /* implicit */ InLargeData(Type& t) : raw(&t) {}
    ~InLargeData() = default;

    InLargeData& operator=(Type* rhs) {
        raw = rhs;
        return *this;
    }

    Type* Get() const {
        return raw;
    }

    Type& operator*() const {
        return *raw;
    }

    Type* operator->() const {
        return raw;
    }

    explicit operator bool() const {
        return raw != nullptr;
    }

private:
    Type* raw;
};

template <typename T, int A>
class OutLargeData {
public:
    static_assert(std::is_trivially_copyable_v<T>, "LargeData type must be trivially copyable");
    static_assert((A & BufferAttr_In) == 0, "OutLargeData attr must not be In");
    static constexpr BufferAttr Attr = static_cast<BufferAttr>(A | BufferAttr_In | BufferAttr_FixedSize);
    using Type = T;

    /* implicit */ OutLargeData(const OutLargeData& t) : raw(t.raw) {}
    /* implicit */ OutLargeData(Type* t) : raw(t) {}
    /* implicit */ OutLargeData(AutoOut<T>& t) : raw(&t.raw) {}
    OutLargeData& operator=(const OutLargeData&) = delete;

    Type* Get() const {
        return raw;
    }

    Type& operator*() const {
        return *raw;
    }

    Type* operator->() const {
        return raw;
    }

    operator Type*() const {
        return raw;
    }

private:
    Type* raw;
};
// clang-format on

} // namespace Service